`timescale 1ns / 1ps

module fifo_burst #(
    parameter DATA_WIDTH = 128,
    parameter FIFO_DEPTH = 256,
    parameter BURST_CNT_WIDTH = 8
)(
    input wire clk,
    input wire rst_n,
    input wire wr_en,
    input wire rd_en,
    input wire [DATA_WIDTH-1:0] din,
    input wire [BURST_CNT_WIDTH-1:0] burst_len,
    
    output reg [DATA_WIDTH-1:0] dout,
    output reg burst_done,
    output reg empty,
    output reg full
);

    reg [DATA_WIDTH-1:0] fifo [0:FIFO_DEPTH-1];
    reg [BURST_CNT_WIDTH-1:0] wr_count;
    reg [BURST_CNT_WIDTH-1:0] rd_count;
    reg [8:0] wr_ptr;  // 指针宽度根据FIFO深度调整
    reg [8:0] rd_ptr;
    reg [8:0] count;   // FIFO中的数据计数

    // 初始化
    integer i;
    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            for (i = 0; i < FIFO_DEPTH; i = i + 1)
                fifo[i] <= 0;
            wr_count <= 0;
            rd_count <= 0;
            wr_ptr <= 0;
            rd_ptr <= 0;
            count <= 0;
            burst_done <= 0;
            empty <= 1'b1;
            full <= 1'b0;
            dout <= 0;
        end
    end

    // 写操作
    always @(posedge clk) begin
        burst_done <= 0;
        
        if (wr_en && !full) begin
            fifo[wr_ptr] <= din;
            wr_ptr <= wr_ptr + 1;
            count <= count + 1;
            
            if (wr_count < burst_len - 1) begin
                wr_count <= wr_count + 1;
            end else begin
                wr_count <= 0;
                burst_done <= 1'b1;
            end
        end
    end

    // 读操作
    always @(posedge clk) begin
        if (rd_en && !empty) begin
            dout <= fifo[rd_ptr];
            rd_ptr <= rd_ptr + 1;
            count <= count - 1;
            
            if (rd_count < burst_len - 1) begin
                rd_count <= rd_count + 1;
            end else begin
                rd_count <= 0;
            end
        end
    end

    // 更新空满状态
    always @(posedge clk) begin
        empty <= (count == 0);
        full <= (count == FIFO_DEPTH);
    end

endmodule